package com.example.huitong.fragments;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.huitong.App;
import com.example.huitong.R;
import com.example.huitong.db.DBImpl;

public class RegisterFragment extends Fragment implements OnClickListener{
	
	private EditText mEtName;
	private EditText mEtPassword;
	private EditText mEtPasswordAgain;
	private EditText mEtPhone;
	private Button mBtnRegist;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_regist, container, false);
		initViews(rootView);
		return rootView;
	}

	private void initViews(View rootView) {
		// TODO Auto-generated method stub
		mEtName = (EditText) rootView.findViewById(R.id.reg_account_et);
		mEtPassword = (EditText) rootView.findViewById(R.id.reg_password_et);
		mEtPasswordAgain = (EditText) rootView.findViewById(R.id.reg_password_again_et);
		mEtPhone = (EditText) rootView.findViewById(R.id.reg_phone_et);
		mBtnRegist = (Button) rootView.findViewById(R.id.reg_btn);
		
		mBtnRegist.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String name = mEtName.getText().toString();
		String pwd1 = mEtPassword.getText().toString();
		String pwd2 = mEtPasswordAgain.getText().toString();
		String phone = mEtPhone.getText().toString();
		if(pwd1 == null || pwd1.equals("") || !pwd1.equals(pwd2)){
			Toast.makeText(getActivity(), "请输入相同密码，且不能为空！", Toast.LENGTH_SHORT).show();
			return;
		}
		App.getApp().showProgressDialog(getActivity(), "");
		new RegisteTask().execute(name, pwd1, phone);
	}
	
	private boolean checkUserNameExsit(String name){
		if(name != null && !"".equals(name)){
			return DBImpl.getInstance().getUser(name) != null;
		}
		return false;
	}
	
	class RegisteTask extends AsyncTask<String, Void, Boolean> {
		private String msg = "";
		private String name;
		
		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			if(params.length != 3){
				throw new IllegalArgumentException("注册参数长度异常");
			}
			try {
				Thread.sleep(1 * 1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String name = params[0];
			String pwd = params[1];
			String phone = params[2];
			this.name = name;
			boolean result = false;
			if(!checkUserNameExsit(name)){
				boolean success = DBImpl.getInstance().addUser(name, pwd, phone);
				if(success){
					msg = "注册成功！！";
					result = true;
				}else {
					msg = "注册失败：数据库错误!";
					result = false;
				}
			}else {
				msg = "注册失败：用户名已存在!";
				result = false;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			App.getApp().dismissDialog();
			if(!result){
				App.toastShort(getActivity(), msg);
			}else {
				App.toastLong(getActivity(), "恭喜，注册成功,请使用新帐号登录！");
				new Handler().postDelayed(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						App.getApp().tmpUserName = name;
						getActivity().finish();
					}
				}, 200);
			}
		}
	}
}
